﻿using System;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace FangDuShi_Admin
{
   
    public class ChatHub : Hub
    {
        //[Authorize]
        private readonly static ConnectionMapping<string> _connections =
           new ConnectionMapping<string>();
        public void Send(string name, string message)
        {
            // Call the addNewMessageToPage method to update clients.

            message = "{'type':'msg','message':'" + message + "'}";
            Clients.Others.addNewMessageToPage(name, message);
           
        }
        public override Task OnConnected()
        {
            string name = Context.User.Identity.Name;

            _connections.Add("123", Context.ConnectionId);

            return base.OnConnected();
        }

        public override Task OnDisconnected()
        {
            string name = Context.User.Identity.Name;

            _connections.Remove(name, Context.ConnectionId);

            return base.OnDisconnected();
        }

        public override Task OnReconnected()
        {
            string name = Context.User.Identity.Name;
           
            if (!_connections.GetConnections(name).ToString().Contains(Context.ConnectionId))
            {
                _connections.Add(name, Context.ConnectionId);
            }

            return base.OnReconnected();
        }
        public static  ConnectionMapping<string> ChatConnection
        {
            get
            {
                return _connections;
            }
        }
    }
}